#!/bin/bash
rm -rf /etc/auth_system

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'

# 检查 root 权限
check_root() {
    if [ "$(id -u)" -ne 0 ]; then
        echo -e "${RED}错误：请使用 root 用户或通过 sudo 运行此脚本${NC}"
        exit 1
    fi
}

# 检测 Docker 是否已安装
check_docker_installed() {
    if command -v docker &>/dev/null; then
        echo -e "${GREEN}✓ Docker 已安装: $(docker --version)${NC}"
        return 0
    else
        return 1
    fi
}

# 检测 Docker Compose 是否已安装
check_compose_installed() {
    if command -v docker-compose &>/dev/null; then
        echo -e "${GREEN}✓ Docker Compose 已安装: $(docker-compose --version)${NC}"
        return 0
    elif docker compose version &>/dev/null; then
        echo -e "${GREEN}✓ Docker Compose 插件已安装: $(docker compose version)${NC}"
        return 0
    else
        return 1
    fi
}

# 分类应用菜单
declare -A CATEGORIES=(
    ["1-媒体服务"]="jellyfin 媒体服务器 emby 媒体服务器 synctv 同步观影 IPTV 电视直播源 libretv libretv影院 moontv moontv影院 katelyatv katelyatv影院 G-box gbox影视超集 xiaoya-tvbox TVBox电视盒子 xiaomusic 小米音箱 \
          "
    ["2-下载工具"]="qbittorrent BT下载工具 xunlei 迅雷下载工具"
    ["3-网盘管理"]="alist 多网盘挂载工具 openlist 多网盘挂载工具"
    ["4-网络工具"]="ddns-go 动态DNS解析工具 lucky 网络工具 FRP 内网穿透工具 xui xui面板 3xui 3xui面板 wireguard V匹N工具 tailscale 组网工具 openvpn V匹N工具 zerotier 虚拟组网工具 easytier 虚拟组网工具 \
                   syncthing 文件同步工具  verysync 微力同步 homeassistant homeassistant家居网关" 
    ["5-远程管理"]="rustdesk 远程桌面服务端 next-terminal 远程访问 tubuntu ssh终端"
    ["6-办公系统"]="gouguoa OA办公系统 onlyoffice 办公套件"
    ["7-实用工具"]="it-tools 实用工具箱 dockerCopilot 容器管理 sun-panel 书签控制面板 photopea 在线PS drawio 图表工具 edit-pdf PDF编辑器 stackedit Markdown编辑器 godoos godoos实用工具"
    ["8-开发相关"]="dockerdaemen dockerdaemen配置 acme.sh 证书管理工具 mysql 数据库 nginx-ui Nginx管理界面"
    ["9-其他工具"]="halo 博客系统 memos 备忘录工具 qinglong 青龙面板 fc-games 经典游戏平台 iventoy 网络启动工具 upsnap 远程唤醒工具 llonebot 机器人框架"
)

# 安装函数
install_app() {
    case $1 in
        # 1-媒体服务
        "jellyfin") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-jellyfin.sh)" ;;
        "emby") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-emby.sh)" ;;
        "synctv") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-synctv.sh)" ;;
        "IPTV") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-iptv.sh)" ;;
        "libretv") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-libretv.sh)" ;;
        "moontv") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-moontv.sh)" ;;
        "katelyatv") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-katelyatv.sh)" ;;
        "G-box") bash -c "$(curl -sSLf https://ailg.ggbond.org/xy_install.sh)" ;;
        "xiaoya-tvbox") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-xiaoya-tvbox.sh)" ;;
        "xiaomusic") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-xiaomusic.sh)" ;;

        #2-下载工具
        "qbittorrent") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-qbittorrent.sh)" ;;
        "xunlei") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-xunlei.sh)" ;;

        #3-网盘管理
        "alist") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-alist.sh)" ;;
        "openlist") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-openlist.sh)" ;;

        #4-网络工具
        "ddns-go") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-ddnsgo.sh)" ;;
        "lucky") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-lucky.sh)" ;;
        "FRP") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-frp.sh)" ;;
        "xui") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-xui.sh)" ;;
        "3xui") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-3xui.sh)" ;;
        "wireguard") bash -c "$(curl -sSLf https://raw.githubusercontent.com/mfkd1000/docker/main/docker-install-wireguard.sh)" ;;
        "tailscale") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-tailscale.sh)" ;;
        "openvpn") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-openvpn.sh)" ;;
        "zerotier")
            docker run --name=zerotier-one --device=/dev/net/tun --net=host --cap-add=NET_ADMIN --cap-add=SYS_ADMIN -d -v /var/lib/zerotier-one:/var/lib/zerotier-one --restart unless-stopped zerotier/zerotier:latest
            read -p "请输入要加入的 ZeroTier 网络ID: " NETWORK_ID
            docker exec zerotier-one zerotier-cli join $NETWORK_ID
            ;;
        "easytier") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-easytier.sh)" ;;
        "syncthing") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-syncthing.sh)" ;;
        "verysync") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-verysync.sh)" ;;
        "homeassistant") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-homeassistant.sh)" ;;

        #5-远程管理
        "rustdesk") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-rustdesk.sh)" ;;
        "next-terminal") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-next-terminal.sh)" ;;
        "tubuntu") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-tubuntu.sh)" ;;

        #6-办公系统
        "gouguoa") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/docker-install-oa.sh)" ;;
        "onlyoffice") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-onlyoffice.sh)" ;;

        #7-实用工具
        "it-tools") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-it-tools.sh)" ;;
        "dockerCopilot") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-dockerCopilot.sh)" ;;
        "sun-panel") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-sun-panel.sh)" ;;
        "drawio") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-drawio.sh)" ;;
        "photopea") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-photopea.sh)" ;;
        "edit-pdf") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-edit-pdf.sh)" ;;
        "stackedit") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-stackedit.sh)" ;;
        "godoos") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-godoos.sh)" ;;


        #8-开发相关
        "dockerdaemen") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/dsm/rewrite_daemen.sh)" ;;
        "acme.sh") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/allupcert.sh)" ;;
        "mysql") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-mysql.sh)" ;;
        "nginx-ui") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-nginx-ui.sh)" ;;
        
        


        #9-其他工具
        "halo") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-halo.sh)" ;;
        "memos") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-memos.sh)" ;;
        "qinglong") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-qinglong.sh)" ;;
        "fc-games") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-fc-games.sh)" ;;
        "iventoy") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-iventoy.sh)" ;;
        "upsnap") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-upsnap.sh)" ;;
        "llonebot") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/docker-install-llonebot.sh)" ;;
        *) echo "暂不支持安装 $1" ;;
    esac
}

# 显示菜单
show_menu() {
    clear
    echo -e "${PURPLE}============================================${NC}"
    echo -e "${PURPLE}          Docker 应用一键安装脚本          ${NC}"
    echo -e "${PURPLE}============================================${NC}"
    echo -e "${CYAN}✓ Docker 状态: $(docker --version 2>/dev/null || echo "未安装")${NC}"
    echo -e "${CYAN}✓ Docker Compose 状态: $(docker-compose --version 2>/dev/null || docker compose version 2>/dev/null || echo "未安装")${NC}"
    echo -e "${PURPLE}--------------------------------------------${NC}"
    echo -e "${YELLOW}请选择要安装的应用 (输入分类编号+应用编号，如801选择第8分类第1个应用):${NC}"
    echo -e "${PURPLE}--------------------------------------------${NC}"
    
    declare -gA APP_MAP  # 声明为全局变量
    
    # 按分类名称排序
    local sorted_categories
    IFS=$'\n' sorted_categories=($(sort <<<"${!CATEGORIES[*]}"))
    
    for category in "${sorted_categories[@]}"; do
        # 提取分类编号和名称
        category_num=${category%%-*}
        category_name=${category#*-}
        
        echo -e "${BLUE}${category_num}. ${category_name}${NC}"
        
        IFS=' ' read -ra APPS <<< "${CATEGORIES[$category]}"
        local app_index=0
        
        # 计算最大名称长度用于对齐
        local max_len=0
        for ((i=0; i<${#APPS[@]}; i+=2)); do
            [ ${#APPS[$i]} -gt $max_len ] && max_len=${#APPS[$i]}
        done
        
        # 显示应用
        for ((i=0; i<${#APPS[@]}; i+=2)); do
            app_index=$((i/2+1))
            APP_MAP["${category_num}$(printf "%02d" $app_index)"]="${APPS[$i]}"
            printf "    ${GREEN}%02d${NC} %-${max_len}s ${YELLOW}- %s${NC}\n" \
                   "$app_index" "${APPS[$i]}" "${APPS[$i+1]}"
        done
        echo
    done
    
    echo -e "${PURPLE}--------------------------------------------${NC}"
    echo -e "${GREEN}0. 退出脚本${NC}"
    echo -e "${PURPLE}--------------------------------------------${NC}"
}

# 主循环
main() {
    check_root
    check_docker_installed || {
        echo -e "${RED}Docker 未安装，请先安装 Docker${NC}"
        exit 1
    }
    check_compose_installed || {
        echo -e "${YELLOW}警告: Docker Compose 未安装，部分应用可能无法正常运行${NC}"
    }
    
    while true; do
        show_menu
        read -p "请输入选择 (如801选择第8分类第1个应用), 或0退出: " CHOICE
        
        if [[ "$CHOICE" == "0" ]]; then
            echo -e "${YELLOW}退出脚本。${NC}"
            exit 0
        elif [[ "$CHOICE" =~ ^[0-9]{3}$ ]]; then
            # 提取分类号(第一位)和应用号(后两位)
            category_num=${CHOICE:0:1}
            app_num=${CHOICE:1:2}
            
            # 去除前导零
            app_num=$((10#$app_num))
            
            # 构建选择键(分类号+两位数应用号)
            SELECT_KEY="${category_num}$(printf "%02d" $app_num)"
            
            SELECTED_APP="${APP_MAP[$SELECT_KEY]}"
            if [[ -n "$SELECTED_APP" ]]; then
                echo -e "${GREEN}您选择了: ${SELECTED_APP}${NC}"
                read -p "确认安装 $SELECTED_APP? [Y/n]: " CONFIRM
                # 修改这里：默认确认安装，只有输入n/N才取消
                if [[ ! "$CONFIRM" =~ ^[nN]$ ]]; then
                    echo -e "${CYAN}开始安装 ${SELECTED_APP}...${NC}"
                    install_app "$SELECTED_APP"
                    read -p "安装完成，按回车键继续..."
                else
                    echo -e "${YELLOW}已取消安装 ${SELECTED_APP}${NC}"
                    sleep 1
                fi
            else
                echo -e "${RED}无效选择，分类${category_num}的应用${app_num}不存在${NC}"
                echo -e "${YELLOW}可用选项: ${!APP_MAP[@]}${NC}"
                sleep 2
            fi
        else
            echo -e "${RED}无效格式，请使用三位数格式 (如801表示第8分类第1个应用)${NC}"
            sleep 2
        fi
    done
}

main